#!/usr/bin/python
# -*- coding: utf-8 -*-
import smtplib
import time
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication

def send_mail(file_new):
    #发送邮箱的服务器
    SMTPserver = 'smtp.126.com'
    #发送的邮箱和密码
    sender = 'kenzop@126.com'
    password = 'shayan123456'
    #目标的邮箱
    destination = 'zhouyi5@metersbonwe.com'

    now = time.strftime('%Y-%m-%d')
    #邮件的标题Subject、发信人From、收件人To
    msg = MIMEMultipart()
    msg["Subject"] = "自动化测试报告" + now
    msg["From"]    = sender
    msg["To"]      = destination

    #正文内容
    f = open(file_new, 'rb')
    mail_body = f.read()
    f.close()

    part = MIMEText(mail_body, 'html', 'utf-8')
    msg.attach(part)

    #附件
    part = MIMEApplication(open(file_new, 'rb').read())
    part.add_header('Content-Disposition', 'attachment', filename=msg["Subject"] + '.html')
    msg.attach(part)

    #连接邮箱
    mailserver = smtplib.SMTP(SMTPserver, 25)
    mailserver.login(sender, password)
    mailserver.sendmail(sender, destination, msg.as_string())
    mailserver.quit()
    print ('send email success')